package cn.walls1717.wallsmall.order.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import cn.walls1717.wallsmall.order.pojo.vo.PayVO;
import lombok.Data;

/**
 * 支付宝付款
 *
 * @author walls1717
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 在支付宝创建的应用的id
    private String app_id = "2021000121690600";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key =
        "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCEwD9/zDJaOY1gVd3gT6BIBZ2bfx9vV4jIt9kzQL3xm20Okf6x/dx+B7Z933sTYWYccns4sk5Xt7H4TDV8U63IX0zJwT+Zf81Kgk2hzV+yMbgMWTbfHiFRYI4dMnZWiYo0wX+GHsZqOQusX543r62jMgpir+Ma37wdezfDGeUjBszOCvYSH0FvkaI8gruKykwZpw0FCT1tX++QcrSEJJ55IHg4Vt+8TzS1TD4IYaQKAjAVI4zpS1Z7YzAK9F5xQv4C/ZM2S9n7rAwdnmnNGnu1d9uMqZA4lL7aFWBsdmHYdORMKI1YXZLt8EJcJoVECUTBEDbdz/DiASoKQqFPuyHfAgMBAAECggEBAIL9oKreNyFIgPR6RkG80I/A2tBBFCet3E8S+sPUMplhoK8r6IKB2TyTJ8P07WEmTrfrTyOW7i8oF/jGWlCf0W9Selbq1XMF5PCMu0XZfYA8BzxIHBj1hjLib9jnnlWtyi5ccRVK8MaIG7ivcVPJijDqg8z1ecde1iTRHSjrOGriMSXqbcfv+A04Zx7ayVyw3iUE54F3X1uhSpFkOBdtIJhvf8xIAWObIboDr/6vsF/IyB6HtmalssWG/V+JvIybXFf0hQIl45GmF9LTM3fWA9gUrzKjULxjNUiz55o9NSi4cooORqEUV4dS7QZby+cYfI6fZqczlnF1veWBLyhUlKECgYEAzDJX3d8MyLQOysN3Cy31u9mNzbFHsSXiJDBuAvwQGMukU5h32Ys1nBLgKDFcmaRqOegRhuvLjIa62galnT0TPlzfYpiBsixxP58dJtsh+BfoYLSI1V1ibJOdAD3VA92cl2CVpW/uPd5GiINFXXEi+MoRahfbUHjIa/f6RtcJECsCgYEApm3T/AUpDU4TVQuOi9V/eOcMQsUO1YDDVPPPAgVYBojTwXPV6aCjQg+9U0j7w+MMLE1HZgjJ+A27mGrrkM28d9NGcB5vcokPNz+Mx7UUkxPkKrUXa+va3A22v1svoO/xu+VY+tExjwBzsk4FhNty5Qbco6v+W8NqXvyj5C4ZZx0CgYEAxnRPLVVwiUdl8O6TrgvW/eDsjnKzqJmW9XEUGlZ8Fn0kkhJmuMkxKyS1Mpr20mQW0XyYPDbCtyriQq+fGfiX0U5V1O8/bbj/v8OWg4MJuqMRt5r+Oc/urnOwbOGrO3yQihbWmdj1BLAYgOiD69LfGEjbL+9LuSQKNdTmKx8msc0CgYBk3hukmtwOnEtLN/3k8jiX5jCgJx6ed7PrQPktmF+JVlRA4xu6xQkteKyeVZWBW5J9JzkmthzyoiAYeHTdBlQ7q2j9evTAziDSs0l3oGAf4+Zdm/PeDMxQol8oCvQK7mHsJ0yOd0s6CGQWpQLjSr2P4WDIMe2vX/iQKT6LhQUQYQKBgCVb7Lp792+Z99tkG+pioCiEgvaHq6TlCyloyVz4Fwm9PoWEH5WWXq5WVBoAKlpVaV6p1E6osUCK9m54TdbueWE8Bdl2MdvmAyh09neIWQivfCvNYeZtuH+SUuqIebNE80YZVCryV7EyhaEn9qcxu8mFDCIJt69dtD4EOQCTOqMh";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key =
        "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuGg1tsCbD5d5IK/mvIrm9TMWDSzuUQmL4zV90T499NxTzl9tBcHiGeGzW700n6LGwlsC+RPu8+6fMZ9LLYFGmxXOp9LWpFb1q+RQsGCSqNBj8amcraZiNuZ+6lAsKlMVlmDsqgZI7WvBvck/9O7ZHrCbN2RkE9SQIJDFiSxjKy9HV5K7UQ9iBIQCom462gBlr9m4PpMsr0uyIeccj3kJYJLBBB/qyBq2bOP2bIl746EJUZZDKsT1AHUTYu3bwh4ZrjVvn3T3kHU2ZgyeB3F4G68980UglEejzSWCuCw2FPbsVWtW/ksvz7ngsHq3kRy0bD1F6hV//yZYmuBOjeIZ5QIDAQAB";
    // 服务器[异步通知]页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://wallsmall.free.idcfengye.com/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.wallsmall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVO vo) throws AlipayApiException {

        // AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id,
        // AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key,
        // AlipayTemplate.sign_type);
        // 1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl, app_id, merchant_private_key, "json", charset,
            alipay_public_key, sign_type);

        // 2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        // 商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOutTradeNo();
        // 付款金额，必填
        String total_amount = vo.getTotalAmount();
        // 订单名称，必填
        String subject = vo.getSubject();
        // 商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"timeout_express\":\"1m\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        // 会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
